package com.sleepcamel.rest.domain.method;

import java.beans.PropertyChangeListener;
import java.util.List;
import java.util.Set;

import com.sleepcamel.rest.domain.BaseMethod;
import com.sleepcamel.rest.domain.IPathable;
import com.sleepcamel.rest.domain.Path;
import com.sleepcamel.rest.domain.method.parameter.MethodParameter;

public class RESTMethod extends BaseMethod<MethodParameter> implements IPathable{

	private RESTMethodType type;
	private Path path;
	private boolean encodable = false;
	private String body = "";
	
	public RESTMethod() {
		setPath(new Path(""));
		setType(RESTMethodType.GET);
	}
	
	public RESTMethodType getType() {
		return type;
	}
	
	public void setType(RESTMethodType type) {
		support.firePropertyChange("type", this.type, this.type = type);
	}
	
	public Path getPath() {
		return path;
	}
	
	public void setPath(Path path) {
		this.path = path;
	}

	public void setProduces(List<String> produces) {
		this.produces = produces;
	}

	public void setConsumes(List<String> consumes) {
		this.consumes = consumes;
	}

	public void setEncodable(boolean encodable) {
		this.encodable = encodable;
	}

	@Override
	public boolean isEncodable() {
		return encodable ;
	}
	
	@Override
	public Set<String> getInterfaceImports() {
		interfaceImports.clear();
		interfaceImports.add(type.getImport());
		
		if ( encodable )
			interfaceImports.add("javax.ws.rs.Encoded");
		
		if ( !produces.isEmpty() )
			interfaceImports.add("javax.ws.rs.Produces");
		
		if ( !consumes.isEmpty() )
			interfaceImports.add("javax.ws.rs.Consumes");
		
		for (MethodParameter parameter : parameters) {
			interfaceImports.addAll(parameter.getInterfaceImports());
		}
		
		return super.getInterfaceImports();
	}
	
	@Override
	public void addPropertyChangeListener(PropertyChangeListener listener) {
		super.addPropertyChangeListener(listener);
		path.addPropertyChangeListener(listener);
	}
	
	@Override
	public void removePropertyChangeListener(PropertyChangeListener listener) {
		super.removePropertyChangeListener(listener);
		path.removePropertyChangeListener(listener);
	}

	public String getMethodBody(){
		return body;
	}

	public void setMethodBody(String body) {
		support.firePropertyChange("body", this.body, this.body  = body);
	}
}
